﻿using System.Web.Mvc;
using Blog.Models;
using Business;
using DAL;

namespace Blog.Controllers
{
    public class ArticlesController : Controller
    {
        //
        // GET: /Articles/1

        public ActionResult Index(int id = 1)
        {
            ArticleIndexViewModel ArticleVM = new ArticleIndexViewModel();
            ArticleVM.Articles = ArticlesManager.Read(id);
            ArticleVM.NbArticles = ArticlesManager.Count();

            ViewBag.Message = "Naviguez dans la liste des articles";

            return View(ArticleVM);
        }

        //
        // GET: /Articles/Create

        [Authorize]
        public ActionResult Create()
        {
            ViewBag.Category = CategoriesManager.Read();

            ViewBag.Message = "Créez votre article";

            return View();
        }

        //
        // POST: /Articles/Create

        [Authorize]
        [HttpPost]
        public ActionResult Create(ArticleCreateViewModel article)
        {
            if (ModelState.IsValid)
            {
                Article a = new Article();
                a.Title = article.Title;
                a.Content = article.Content;
                a.CategoryID = article.CategoryID;

                ArticlesManager.Create(a);
                return RedirectToAction("Index");
            }
            else
            {
                ViewBag.Category = CategoriesManager.Read();
                return View();
            }
        }

        //
        // GET: /Articles/Details/5

        public ActionResult Details(int id)
        {
            Article article = new Article();
            article = ArticlesManager.ReadOne(id);
            if (article == null)
                return RedirectToAction("Index", "Articles");

            Comment comment = new Comment();
            comment.Content = "";
            comment.ArticleID = article.ID;

            ArticleDetailsViewModel ArticleVM = new ArticleDetailsViewModel();
            ArticleVM.Article = article;
            ArticleVM.Username = comment.Username;
            ArticleVM.Content = comment.Content;
            ArticleVM.ArticleID = comment.ArticleID;

            ViewBag.Message = "Informations sur cet article";

            return View(ArticleVM);
        }

        //
        // GET: /Articles/Edit/5

        [Authorize]
        public ActionResult Edit(int id)
        {
            Article article = new Article();
            article = ArticlesManager.ReadOne(id);

            ViewBag.Category = CategoriesManager.Read();
            ViewBag.Message = "Modifiez les informations";

            ArticleCreateViewModel articleVM = new ArticleCreateViewModel();
            articleVM.ID = article.ID;
            articleVM.Title = article.Title;
            articleVM.Content = article.Content;
            articleVM.CategoryID = article.CategoryID;

            return View(articleVM);
        }

        //
        // POST: /Articles/Edit/5

        [Authorize]
        [HttpPost]
        public ActionResult Edit(ArticleCreateViewModel article)
        {
            if (ModelState.IsValid)
            {
                Article a = new Article();
                a.ID = article.ID;
                a.Title = article.Title;
                a.Content = article.Content;
                a.CategoryID = article.CategoryID;

                ArticlesManager.Update(a);
                string url = Request.ServerVariables["HTTP_REFERER"];
                string[] urlArray = url.Split('/');

                return RedirectToAction("Details/" + urlArray[5], "Articles");
            }
            else
            {
                ViewBag.Category = CategoriesManager.Read();
                return View(article);
            }
        }

        //
        // GET: /Articles/Delete

        [Authorize]
        public ActionResult Delete(int id)
        {
            ArticlesManager.Delete(id);
            return RedirectToAction("Index");
        }

        [HttpPost]
        public ActionResult Search(int id = 1)
        {
            string search = Request.Form["search-input"];
            ViewBag.Message = "Résultat de votre recherche";
            ViewBag.search = search;

            return View(ArticlesManager.Search(search));
        }
    }
}
